<template>
  <div ref="main">
    <Header :active-index="2"/>
    <div class="category" v-if="categoryList.length>0">
      <div class="category-list">
        <el-tag size="small" :effect="effectAll" class="category-item" @click="selectAll">全部</el-tag>
        <el-tag size="small" class="category-item"  v-for="(item,index) in categoryList" :key="item.categoryId" @click="selectItem(item.categoryId,index)" :effect="tagIndex === index ? 'dark' : 'plain'">{{item.categoryName}}</el-tag>
      </div>
      <ArticleList :no-scroll="noScroll" :article-list="articleList"/>
    </div>
    <div v-else>
      <h3 style="text-align: center">暂无分类信息</h3>
    </div>
    <Footer/>
  </div>
</template>

<script>
import Footer from '@/components/Footer'
import Header from '@/components/Header'
import ArticleList from '@/components/ArticleList'
export default {
  name: 'Category',
  components: { Footer, Header, ArticleList },
  data () {
    return {
      categoryList: [],
      articleList: [],
      noScroll: false,
      page: 1,
      scrollTop: '',
      clientHeight: '',
      categoryId: '',
      tagIndex: -1,
      effectAll: 'dark'
    }
  },
  methods: {
    getCategoryList () {
      this.$http.get('/api/category/list').then(data => {
        this.categoryList = [...data.data]
      })
    },
    getArticleList () {
      let url = `api/article/page/${this.page}`
      if (this.categoryId) {
        url = `api/article/category/${this.categoryId}/${this.page}`
      }
      this.$http.get(url).then(data => {
        if (data.data.current <= data.data.pages) {
          if (this.page === 1) {
            this.articleList = [...data.data.records]
          } else {
            this.articleList.push(...data.data.records)
          }
          this.page++
        } else {
          this.noScroll = true
        }
      })
    },
    handleScroll (e) {
      this.scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      if (this.$refs.main.scrollHeight - this.scrollTop === this.clientHeight) {
        if (this.noScroll) {
          e.preventDefault()
        } else {
          this.getArticleList()
        }
      }
    },
    selectAll () {
      this.page = 1
      this.noScroll = false
      this.effectAll = 'dark'
      this.tagIndex = -1
      this.categoryId = ''
      this.getArticleList()
    },
    selectItem (categoryId, index) {
      this.page = 1
      this.noScroll = false
      this.categoryId = categoryId
      this.effectAll = 'plain'
      this.tagIndex = index
      this.getArticleList()
    }
  },
  created () {
    this.$nextTick(() => {
      window.addEventListener('scroll', this.handleScroll, true)

      this.clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight

      this.getCategoryList()

      this.getArticleList()
    })
  }
}
</script>

<style scoped lang="scss">
.category{
  padding-top: 70px;
  margin: 0 auto;
  width: 780px;
  min-height: calc(100vh - 150px);
  .category-list{
    display: flex;
    flex-wrap: wrap;
    .category-item{
      margin: 5px;
      cursor: pointer;
    }
  }
}
</style>
